package com.tf.temper.controller;

import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.temper.entity.*;
import com.tf.temper.service.LogeventService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 获取异常的体温
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "获取异常的体温",description="GetTemperController")
@RestController
public class GetTemperController extends BaseController
{
    @Autowired
    private LogeventService<LogeventEntity> logeventService;
    @ApiOperation("获取异常的体温人数接口")
    @GetMapping("/feverPeopleList")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StatisticsReturnEntity<TemperReturnEntity[]> feverPeopleList(@RequestParam int size,@RequestParam int page)
    {
        EntityWrapperHandle<LogeventEntity> ewcount = new EntityWrapperHandle<LogeventEntity>();
        //ewcount.eq("opencode",0);
        int count = logeventService.selectCount(ewcount);
        if(page>=1&&size>0&&size<=200&&count>0)
        {
            if( (count -  (page-1)*size) >0 )
            {
                EntityWrapperHandle<LogeventEntity> ew = new EntityWrapperHandle<LogeventEntity>();
                ew.orderDesc(Arrays.asList(new String[] {"id"}));
                //ew.eq("opencode",0);
                ew.last("limit "+(page-1)*size+","+size);
                List<LogeventEntity> normalNum =  logeventService.selectList(ew);
                TemperReturnEntity[] data = null;
                if(size>0&&size<=200&&normalNum!=null&&normalNum.size()>0)
                {
                    String format = "yyyy-MM-dd HH:mm:ss";
                    SimpleDateFormat sdf = new  SimpleDateFormat(format);
                    int maxlen = 0;
                    if(size>=normalNum.size())
                    {
                        maxlen = normalNum.size();
                    }
                    else
                    {
                        maxlen = size;
                    }
                    data = new TemperReturnEntity[maxlen];
                    for (int i= 0;i<maxlen;++i)
                    {
                        TemperReturnEntity dataval = new TemperReturnEntity();
                        dataval.setId(normalNum.get(i).getId());
                        dataval.setDeviceId(normalNum.get(i).getUuid());
                        dataval.setTemperature(Double.parseDouble(normalNum.get(i).getTemperature()));
                        dataval.setName(normalNum.get(i).getUserName());
                        dataval.setTime(sdf.format(normalNum.get(i).getCreatetime()));
                        data[i] = dataval;
                    }
                }
                else
                {
                    data = new TemperReturnEntity[1];
                }
                return  new StatisticsReturnEntity<TemperReturnEntity[]>().ok(data,count);
            }
        }
        return  new StatisticsReturnEntity<TemperReturnEntity[]>().error("size or page err");
    }
}